package mylittleinvestigations.Collisions;

import java.util.ArrayList;
import mylittleinvestigations.Vector2;

public class Collision
{
    private static void CalculateInterval(ArrayList<Vector2> vertices, Vector2 axis, CalculateIntervalParameter param)
    {
        param.MinDistance = (float)axis.dot(vertices.get(0));
        param.MaxDistance = param.MinDistance;

        for (int i = 1; i < vertices.size(); i++)
        {
            float distance = (float)axis.dot(vertices.get(i));

            if (distance < param.MinDistance)
            {
                param.MinDistance = distance;
            }
            else if (distance > param.MaxDistance)
            {
                param.MaxDistance = distance;
            }
        }
    }

    // Determines whether there's an intersection between the vertices along the given axis,
    // and returns how far they're overlapping if so.
    private static boolean IsIntervalIntersection(ArrayList<Vector2> polygon1Vertices, ArrayList<Vector2> polygon2Vertices, Vector2 offset, Vector2 axis, CollisionParameter param)
    {
        float minDistance1;
        float maxDistance1;
        float minDistance2;
        float maxDistance2;
        
        CalculateIntervalParameter param1 = new CalculateIntervalParameter();
        CalculateIntervalParameter param2 = new CalculateIntervalParameter();

        CalculateInterval(polygon1Vertices, axis, param1);
        CalculateInterval(polygon2Vertices, axis, param2);
        
        minDistance1 = param1.MinDistance;
        maxDistance1 = param1.MaxDistance;
        minDistance2 = param2.MinDistance;
        maxDistance2 = param2.MaxDistance;

        float h = (float)offset.dot(axis);
        minDistance1 += h;
        maxDistance1 += h;

        float distance1 = minDistance1 - maxDistance2;
        float distance2 = minDistance2 - maxDistance1;

        if (Math.abs(distance1) < 0.01f)
        {
            distance1 = 0;
        }

        if (Math.abs(distance2) < 0.01f)
        {
            distance2 = 0;
        }

        param.OverlapDistance = Math.max(distance1, distance2);
        return distance1 <= 0 && distance2 <= 0;
    }

    // Determines whether there's an intersection between the given objects,
    // and returns the distance they're overlapping and along which axis if so.
    public static boolean IsCollision(CollidableObject collisionObject1, Vector2 collisionObject1Offset, CollidableObject collisionObject2, Vector2 collisionObject2Offset, CollisionParameter param)
    {
        param.OverlapAxis = new Vector2(0, 0);
        param.OverlapDistance = Float.NEGATIVE_INFINITY;

        int axesCount = 0;
        Vector2[] axes = new Vector2[32];

        for (int i = 0; i < collisionObject1.getNormals().size(); i++)
        {
            axes[axesCount++] = collisionObject1.getNormals().get(i);
        }

        for (int i = 0; i < collisionObject2.getNormals().size(); i++)
        {
            axes[axesCount++] = collisionObject2.getNormals().get(i);
        }

        Vector2 collisionObjectsSeparation = collisionObject1.getPosition().add(collisionObject1Offset).subtract(collisionObject2.getPosition().add(collisionObject2Offset));

        for (int i = 0; i < axesCount; i++)
        {
            CollisionParameter tempParam = new CollisionParameter();

            if (!IsIntervalIntersection(collisionObject1.getVertices(), collisionObject2.getVertices(), collisionObjectsSeparation, axes[i], tempParam))
            {
                return false;
            }

            if (tempParam.OverlapDistance < 0)
            {
                float overlapDistance = tempParam.OverlapDistance;
                Vector2 overlapAxis = axes[i];
                
                if (overlapAxis.scalarMultiply(overlapDistance).dot(collisionObjectsSeparation) < 0)
                {
                    overlapAxis = overlapAxis.scalarMultiply(-1);
                }
                
                if (param.OverlapDistance < 0 && tempParam.OverlapDistance > param.OverlapDistance)
                {
                    param.OverlapDistance = overlapDistance;
                    param.OverlapAxis = overlapAxis;
                }
                
                param.AddOverlapEntry(overlapDistance, overlapAxis, collisionObject1, collisionObject2);
            }
            else
            {
                float overlapDistance = tempParam.OverlapDistance;
                Vector2 overlapAxis = axes[i];
                
                if (overlapDistance > param.OverlapDistance)
                {
                    param.OverlapDistance = overlapDistance;
                    param.OverlapAxis = overlapAxis;
                }
                
                param.AddOverlapEntry(overlapDistance, overlapAxis, collisionObject1, collisionObject2);
            }
        }

        return true;
    }
}